
/**
 * js异步加载
 *
 * @param src           js文件路径
 * @param callbackfunc  js执行完的回调函数
 */
export const jsAsyncLoad = (src, callbackfunc) => {
  const body = document.getElementsByTagName('body')[0];
  const script = document.createElement('script');
  script.id = `theme-plugin-${new Date().getTime()}`;
  script.type = 'text/javascript';
  script.src = src;
  if (script.readyState) { // IE
    script.onreadystatechange = () => {
      if (script.readyState == 'loaded' || script.readyState == 'complete') {
        script.onreadystatechange = null;
        if (callbackfunc && typeof callbackfunc == 'function') callbackfunc();
      }
    };
  } else {
    script.onload = () => {
      if (callbackfunc && typeof callbackfunc == 'function') callbackfunc();
    };
  }
  body.appendChild(script);
};

/**
 * css异步加载
 *
 * @param href          css文件路径
 * @param callbackfunc  css加载完的回调函数
 */
export const cssAsyncLoad = (href, callbackfunc) => {
  const body = document.getElementsByTagName('body')[0];
  const link = document.createElement('link');
  link.id = `theme-plugin-${new Date().getTime()}`;
  link.rel = 'stylesheet';
  link.type = 'text/css';
  link.href = href;
  if (link.readyState) { // IE
    link.onreadystatechange = () => {
      if (link.readyState == 'loaded' || link.readyState == 'complete') {
        link.onreadystatechange = null;
        if (callbackfunc && typeof callbackfunc == 'function') callbackfunc();
      }
    };
  } else {
    link.onload = () => {
      if (callbackfunc && typeof callbackfunc == 'function') callbackfunc();
    };
  }
  body.appendChild(link);
};

/**
 * 格式化菜单数据
 *
 * @param data 菜单数据
 * @returns {Array}
 */
export const formatMenuData = (data) => {
  let result = [];

  for (let i = 0, len = data.length; i < len; i++) {
    const item = data[i] || {};
    const parentId = item.parentId || '0';
    const isPortalMenu = !!item.hpid;
    const url = item.linkAddress || item.url || '';
    result.push({
      hpid: item.hpid || '',
      infoId: item.infoId || item.hpid,
      numId: isPortalMenu ? `portal_${item.hpid}` : item.infoId,
      id: `${(isPortalMenu ? 'portal_' : '') + parentId}-${item.infoId || item.id}`,
      parentId,
      levelid: `${parentId}-${isPortalMenu ? (item.levelid && item.pid == '0' ? `${item.levelid.split('_')[0] % 20}_portal` : item.levelid || '0') : item.levelid}`,
      icon: !item.icon || item.icon == '/images/homepage/baseelement_wev8.gif' || item.icon.indexOf('/images_face/') != -1 || item.icon.indexOf('/images/') != -1 || item.icon.indexOf('/image_secondary/') != -1 ? '' : item.icon,
      name: item.name || '',
      pinyin: item.pinyin || '',
      url: url && url.replace(/&#38;/g, '&'),
      routeurl: item.routeurl || '',
      mobxrouteurl: item.mobxrouteurl || '',
      fullrouteurl: item.fullrouteurl || '',
      target: item.target || 'mainFrame',
      child: item.child ? formatMenuData(item.child) : [],
      count: item.count != undefined ? `${item.count}` : '',
      countId: item.countId || '',
      titleUrlIcon: item.titleUrlIcon || '',
      titleUrl: item.titleUrl || '',
      tagColor: item.tagColor || '',
      hasTopLine: item.hasTopLine || '',
    });
  }

  return result;
};

/**
 * 设置字体大小
 *
 * @param fontSize 字体大小
 */
export const setFontSize = (fontSize) => {
  jQuery('#theme-font').remove();
  const href = `/cloudstore/resource/pc/com/font-size/${fontSize}px.css`;
  jQuery(jQuery('head')[0]).append(`<link id="theme-font" rel="stylesheet" type="text/css" href="${href}" />`);
};

/**
 * 获取E9主题样式
 *
 * @param data E9主题样式配置数据
 */
export const getE9ThemeCss = (data) => {
  const { header = {}, toplogo = {}, frequsemenu = {}, quicksearch = {}, leftmenuswitch = {}, leftmenu = {} } = data;

  const css = [];
  css.push(`.e9header-container {color: ${header.color}; font-size: ${header.fontSize}; font-family: ${header.fontFamily}; background: ${header.background};} `);
  css.push(`.e9header-container .e9header-top-logo {color: ${toplogo.color}; font-size: ${toplogo.fontSize}; font-family: ${toplogo.fontFamily}; background: ${toplogo.background};} `);

  css.push(`.e9header-container .e9header-freq-use-menu {background: ${frequsemenu.background};} `);
  css.push(`.e9header-container .e9header-freq-use-menu:hover {background: ${frequsemenu.hoverBackground};} `);

  css.push(`.e9header-container .e9header-quick-search {background: ${quicksearch.background};} `);
  css.push(`.e9header-container .e9header-quick-search:hover {background: ${quicksearch.hoverBackground};} `);

  css.push('.e9aside-container [class*=wevicon-menu],');
  css.push(`.e9aside-container [class^=wevicon-menu] {font-family: ${leftmenu.iconstyle == 'line' ? 'wevicon-line' : 'wevicon'} !important;} `);

  css.push('.e9aside-container .wea-menu-inline,');
  css.push('.e9aside-container .ant-menu-inline,');
  css.push('.e9aside-container .wea-menu-vertical,');
  css.push(`.e9aside-container .ant-menu-vertical {background: ${leftmenu.background};} `);

  css.push('.e9aside-container .wea-menu-inline .wea-menu-switch,');
  css.push(`.e9aside-container .wea-menu-vertical .wea-menu-switch {background: ${leftmenuswitch.background};} `);

  css.push('.e9aside-container .wea-menu-inline .wea-menu-switch i,');
  css.push(`.e9aside-container .wea-menu-vertical .wea-menu-switch i {color: ${leftmenuswitch.color};} `);

  css.push('.e9aside-container .wea-menu-inline .ant-menu-item,');
  css.push(`.e9aside-container .wea-menu-inline .ant-menu-submenu-title {color: ${leftmenu.titleColor}; font-family: ${leftmenu.titleFontFamily};} `);

  css.push('.e9aside-container .wea-menu-inline .ant-menu-item .wea-menu-title,');
  css.push('.e9aside-container .wea-menu-inline .ant-menu-submenu-title .wea-menu-title,');
  css.push('.e9aside-container .wea-menu-vertical .ant-menu-item .wea-menu-title,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-submenu-title .wea-menu-title {font-size: ${leftmenu.titleFontSize};} `);

  css.push('.e9aside-container .wea-menu-inline .ant-menu-item .wevicon,');
  css.push('.e9aside-container .wea-menu-inline .ant-menu-submenu-title .wevicon,');
  css.push('.e9aside-container .wea-menu-vertical .ant-menu-item .wevicon,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-submenu-title .wevicon {color: ${leftmenu.iconColor};} `);

  css.push('.e9aside-container .wea-menu-inline .wea-menu-selected,');
  css.push('.e9aside-container .wea-menu-inline .ant-menu-item:hover,');
  css.push(`.e9aside-container .wea-menu-inline .ant-menu-submenu-title:hover {color: ${leftmenu.selectedTitleColor} !important; background: ${leftmenu.selectedTitleBackground} !important;} `);

  css.push('.e9aside-container .wea-menu-inline .wea-menu-selected .wevicon,');
  css.push('.e9aside-container .wea-menu-inline .ant-menu-item:hover .wevicon,');
  css.push(`.e9aside-container .wea-menu-inline .ant-menu-submenu-title:hover .wevicon {color: ${leftmenu.selectedIconColor} !important;} `);

  css.push('.e9aside-container .wea-menu-vertical .ant-menu-root > .ant-menu-item-active:before,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-root > .ant-menu-submenu-active:before {border: 2px solid ${leftmenu.selectedTitleBackground}; border-top: 21px solid ${leftmenu.selectedTitleBackground}; border-bottom: 21px solid ${leftmenu.selectedTitleBackground};} `);

  css.push('.e9aside-container .wea-menu-vertical .ant-menu-root > .ant-menu-item-active,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-root > .ant-menu-submenu-active {color: inherit; background: ${leftmenu.vBackground};} `);

  css.push('.e9aside-container .wea-menu-vertical .wea-menu-selected .wevicon,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-submenu-selected .wevicon {color: ${leftmenu.vSelectedIconColor} !important;} `);

  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-sub {color: ${leftmenu.vColor}; background: ${leftmenu.vBackground};} `);

  css.push('.e9aside-container .wea-menu-vertical .wea-menu-selected,');
  css.push('.e9aside-container .wea-menu-vertical .ant-menu-submenu-selected,');
  css.push('.e9aside-container .wea-menu-vertical .ant-menu-sub .ant-menu-item-active,');
  css.push(`.e9aside-container .wea-menu-vertical .ant-menu-sub .ant-menu-submenu-active {color: ${leftmenu.vSelectedTitleColor} !important; background: ${leftmenu.vSelectedTitleBackground} !important;} `);

  css.push(`.e9left-menu-email-item {color: #fff; background: ${header.background};} `);
  css.push('.e9left-menu-email-item-selected {color: #003666; background: #f6f6f6;} ');
  css.push('.e9left-menu-email-item-ul {color: #003666; background: #f6f6f6;} ');
  css.push('.e9left-menu-email-item-ul li:hover {background: #dadfe3;} ');

  css.push('.e9theme-layout2-navbar .pt-menu-root > .pt-menu-submenu-active > .pt-menu-submenu-title,');
  css.push('.e9theme-layout2-navbar .pt-menu-root > .pt-menu-submenu-selected > .pt-menu-submenu-title,');
  css.push('.e9theme-layout2-navbar .pt-menu-root > .pt-menu-item-active > .pt-menu-item-title,');
  css.push(`.e9theme-layout2-navbar .pt-menu-root > .pt-menu-item-selected > .pt-menu-item-title {color: #fff; background: ${header.background};} `);

  css.push('.e9theme-layout3-left .pt-menu-root > .pt-menu-submenu-active > .pt-menu-submenu-title,');
  css.push('.e9theme-layout3-left .pt-menu-root > .pt-menu-submenu-selected > .pt-menu-submenu-title,');
  css.push('.e9theme-layout3-left .pt-menu-root > .pt-menu-item-active > .pt-menu-item-title,');
  css.push(`.e9theme-layout3-left .pt-menu-root > .pt-menu-item-selected > .pt-menu-item-title {color: #fff; background: ${header.background};} `);

  return css.join('');
};

/**
 * 设置E9主题样式
 *
 * @param data E9主题样式配置数据
 */
export const setE9ThemeStyle = (data) => {
  const css = data ? getE9ThemeCss(data) : '';
  if (jQuery('#theme-style') && jQuery('#theme-style').length) {
    jQuery('#theme-style').html(css);
  } else {
    jQuery(jQuery('head')[0]).append(`<style id="theme-style" type="text/css">${css}</style>`);
  }
};
